/*  PLUTO JS -Scripting Web Server.
    Copyright (C) 2013 vivek gangadharan

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
package pluto.core;

import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Launcher implements Runnable {

    private String[] arg;

    public Launcher(String[] arg) {
        this.arg = arg;
    }

    @Override
    public void run() {
        ApplicationConfiguration config = null;
        if (arg != null && arg.length > 0) {
            try {
                config = new ApplicationConfiguration();

                for (int i = 0; i < arg.length; i++) {
                    try {
                        String swt = arg[i];
                        if (i + 1 >= arg.length) {
                            System.err.println("Cannot find option(" + swt + ") value");
                        }
                        String a = arg[++i];
                        switch (swt) {
                            case "-root":
                                config.root(a);
                                break;
                            case "-app":
                                config.app(a);
                                break;
                            case "-host":
                                config.host(a);
                                break;
                             case "-port":
                                config.port(a);
                                break;
                            case "-log":
                                config.log(a);
                                break;
                            case "-web":
                                config.web(a);
                                break;
                            case"-jss":
                                config.jss(a);
                            case "-lib":
                                config.lib(a);
                                break;
                            case "-dirlist":
                                config.allowDirListing(true);
                                break;
                            case "-appgroup":
                                config.cluster(a);
                                break;
                            case "-syncrate":
                                config.syncRate(a);
                                break;                           
                            case "-ssl":
                                config.sslKeyStroke(a);
                                break;        
                            case "-storepwd":
                                config.sslKeyStrokePassword(a);
                                break;
                            case "-keypwd":
                                config.sslKeyStrokePassword(a);
                                break;
                           
                        }

                    } catch (IOException ex) {
                        ex.printStackTrace();
                    } catch (Exception ex) {
                        ex.printStackTrace();
                    }
                    
                }
                if (config != null) {
                        try {
                            Host host = new Host(config);
                            (new Thread(host)).start();
                        } catch (Exception ex) {
                            ex.printStackTrace();
                        }
                    }
            } catch (Exception ex) {
                Logger.getLogger(Launcher.class.getName()).log(Level.SEVERE, null, ex);
            }
        }



    }

    public static void main(String arg[]) throws Exception {
        Launcher l = new Launcher(arg);
        l.run();
    }
}
